<?php
class BlogsController extends AppController {
	var $name = 'Blogs';
	var $uses = array('User', 'Blog', 'Comment', 'Like');
	
	var $paginate = array(
		'limit' => 5,
		'order' => array(
			'Blog.modified' => 'desc'
		),
		'recursive' => -1
	);
	
	//myBlogs funtion ats as Blogsontroller "index"
	function myBlogs($userId = null) {
        $user = $this->Session->read('user');
        /*
        $blogs = $this->Blog->find('list', 
        	array('fields' => array('Blog.id', 'Blog.subject'),
        	'conditions' => array('user_id' => $user['User']['id']),
        	'order' => array('modified' => 'DESC')));
        */
		
		$blogs = $this->paginate('Blog', array('Blog.user_id =' => $user['User']['id']));      
        //$this->log($blogs, 'debug');
		$this->set(compact('blogs', 'user'));
	}
	
	function addBlog() {
		$user = $this->Session->read('user');		
		if (!empty($this->data)) {		
			$this->Blog->set('user_id', $user['User']['id']);
			$this->Blog->save();
			if ($this->Blog->save($this->data)) {
				$this->Session->setFlash('Blog Added!');
				$this->redirect(array('action' => 'myBlogs/' .$user['User']['id']));
			}
		}
	}
	
	function editBlog($blogId = null, $userId = null) {
		$this->Blog->id = $blogId;
		$this->set(compact('userId'));
		$user = $this->Session->read('user');
		if (empty($this->data)) {
			$this->data = $this->Blog->read();
		}		
		else {
			if ($this->Blog->save($this->data)) {
				$this->Session->setFlash('Blog Updated!');
				//rediret works only with the session->read funtion,
				//for some reason it doesnt with the editBlog funtion parameter $userId
				$this->redirect(array('action' => 'myBlogs/' .$user['User']['id'   ]));
			}
		}
	}

	function viewBlog($blogId = null) {
		$user = $this->Session->read('user');
		$this->set(compact('user'));
		$this->Blog->id = $blogId;
		$this->set('blog', $this->Blog->read());

		$likeComments = $this->Comment->query(
			"SELECT Comment.id, Comment.blog_id, Comment.blog_id, Comment.user_id, Comment.comment, Comment.created, Comment.modified
			FROM comments Comment, likes l
			WHERE Comment.id = l.comment_id && l.user_id = " .$user['User']['id']);

		$commentCount = $this->Comment->find('count',
			array('conditions' => array('Comment.blog_id' => $blogId),
				'order' => array('Comment.modified' => 'DESC'),
				'recursive' => '-1' )
			);
		$comments = $this->Comment->find('all',
			array('conditions' => array('Comment.blog_id' => $blogId),
				'order' => array('Comment.modified' => 'DESC'),
				'recursive' => '-1' )
			);

		for ($i=0; $i < $commentCount; $i++) {
			if (in_array($comments[$i], $likeComments)) {
				$comments[$i]['Comment']['like'] = 1;
			}
			else
				$comments[$i]['Comment']['like'] = 0;
			
			$likeCount = $this->Like->query(
				"SELECT COUNT('like')
				FROM likes
				WHERE comment_id = " .$comments[$i]['Comment']['id']
			);
			
				
			$like_count = $likeCount[0][0]["COUNT('like')"];	
			$comments[$i]['Comment']['like_count'] = $like_count;	
		}
		
		$this->set(compact('comments'));				
	}

	function deleteBlog($blogId = null) {
		$user = $this->Session->read('user');
		$this->Blog->id = $blogId;
		$blog = $this->Blog->read();
		
		
		$this->Blog->delete($blogId);  //$_REQUEST['param']
		$this->Comment->delete(null);
		/*
		 * With AJAX, redireting and setting flash is essentially pointless...
		 * 		
		 * 
		if ($this->Blog->delete($blogId)) {
			$this->Session->setFlash('Blog "' .$blog['Blog']['subjet'] .'" has been deleted.' );
			$this->rediret(array('ation' => 'myBlogs/' .$user['User']['id']));
		}
		 * 
		 */
	}

}
?>